<?php

namespace app\index\traits;


use app\common\model\MemberInfo;
use app\index\dao\MemberMatchRecordDao;
use ShidianUtil\Redis\RedisClient;

/**
 *
 * Trait MatchRecorderTrait
 * @package app\index\traits
 */
trait MatchRecorderTrait
{
    private $cacheKeyPre = 'lawyer_match_times:';

    /**
     * 记录一次咨询匹配
     * @param $memberId
     * @param $consultingId
     * @param $matchType
     * @return void
     */
    private function recordMatch($memberId, $consultingId, $matchType): void
    {
        $this->getMemberMatchRecordDao()->insert($memberId, $consultingId, $matchType);
    }

    /**
     * @param $lawyerId
     * @param int $number
     * @return void
     */
    private function incMatchTimes($lawyerId, int $number = 1): void
    {
        // 插入数据库
        MemberInfo::where('memberId', $lawyerId)
            ->inc('rushTimes')
            ->update();

        $key = $this->cacheKeyPre . date('Ymd');

        $this->getRedis()->zIncrBy($key, $number, $lawyerId);
    }

    /**
     * 获取匹配次数超过一定的律师
     * @param int $number
     * @return array
     */
    private function getLawyerIdsMatchMoreThan(int $number = 3): array
    {
        $key = $this->cacheKeyPre . date('Ymd');

        return $this->getRedis()->zRangeByScore($key, $number, 99999999);
    }

    private function getRedis(): RedisClient
    {
        return app(RedisClient::class);
    }

    private function getMemberMatchRecordDao(): MemberMatchRecordDao
    {
        return app(MemberMatchRecordDao::class);
    }
}